/* 格式化时间戳 */
export function formatTime (time, isAll) {
  if (!time) {
    return
  }
  const date = new Date(time)
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  let str = `${year}-`
  str += `${month.toString().padStart(2, 0)}-`
  str += `${day.toString().padStart(2, 0)}`
  if (isAll) {
    const hour = date.getHours()
    const min = date.getMinutes()
    const seconds = date.getSeconds()
    str += ` ${hour.toString().padStart(2, 0)}:`
    str += `${min.toString().padStart(2, 0)}:`
    str += `${seconds.toString().padStart(2, 0)}`
  }
  return str
}
/* 时间转化 */
export function formatTimeHD (time) {
  if (!time) {
    return
  }
  var days  = time / 60 / 60 / 24
  var daysRound   = Math.floor(days)
  var hours  = (time - 24 * daysRound *60 *60) / 60 / 60
  var hoursRound   = Math.floor(hours)
  var minutes   = (time - 24 * daysRound *60 *60 -60*60*hoursRound) / 60 
  var minutesRound  = Math.floor(minutes)
  var seconds  = time - (24 * 60 * 60 * daysRound) - (60 * 60 * hoursRound) - (60 * minutesRound)
  let str = daysRound+'天' + hoursRound+'小时'+ minutesRound+'分钟' +seconds+'秒'
 
  return str
}
/* 格式化时间戳 */
export function formatTimeYM (time) {
  if (!time) {
    return
  }
  const date = new Date(time)
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  let str = `${year}-`
  str += `${month.toString().padStart(2, 0)}`
  return str
}
export function formatTimeTs (date) {
  date = new Date(date)
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  return [year, month, day].map(formatNumber).join('-')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

// 下载文件
export function downloadExcel (data, excelName) {
  let blob = new Blob([data])
  if (window.navigator && window.navigator.msSaveOrOpenBlob) {
    navigator.msSaveBlob(blob, excelName + '.xlsx')
  } else {
    const url = window.URL.createObjectURL(new Blob([data]))
    const link = document.createElement('a')
    link.style.display = 'none'
    link.href = url
    link.setAttribute('download', excelName + '.xlsx')
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
  }
}

/**
* 处理对象参数值，排除对象参数值为”“、null、undefined，并返回一个新对象
**/
export function dealObjectValue(obj){
  var param = {};
  if ( obj === null || obj === undefined || obj === "" ) return param;
  for ( var key in obj ){
      if ( obj[key] !== null && obj[key] !== undefined && obj[key] !== "" ){
          param[key] = obj[key];
      }
  }
  return param;
  var param = {};
  if ( obj === null || obj === undefined || obj === "" ) return param;
  for ( var key in obj ){
      if ( dataType(obj[key]) === "Object" ){
          param[key] = dealObjectValue(obj[key]);
      }else if(  obj[key] !== null && obj[key] !== undefined && obj[key] !== ""  ){
          param[key] = obj[key];
      }
  }
  return param;
}

// 默认头像地址
//export const defaultAvatar = require('@/assets/head.png')
